package com.cn.user.service;

import com.cn.api.UserService;
import com.cn.dto.LoginParams;
import com.cn.dto.Permission;
import com.cn.dto.UserDto;
import com.cn.user.mapper.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
@RestController
@RequestMapping("user")
@Component
public class
UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @GetMapping("update")
    public int update(UserDto userDto) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = userDto.getPassword();
        userDto.setPassword(passwordEncoder.encode(password));
        int update = userDao.update(userDto);
        return update;

    }

    @Override
    @PostMapping("insert")
    public int insert(UserDto userDto) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = userDto.getPassword();
        userDto.setPassword(passwordEncoder.encode(password));
        int insert = userDao.insert(userDto);
        return insert;
    }

    @Override
    public List<Permission> getPermissionsByUserId(Integer id) {

        return null;
    }

    @Override
    @PostMapping("status")
    public int status(int id) {
        int status = userDao.status(id);
        return status;

    }

    @Override
    @GetMapping("findById")
    public UserDto findById(int id) {
        UserDto byId = userDao.findById(id);
        return byId;
    }

    @Override
    @PostMapping("delete")
    public int delete(int id) {
        int delete = userDao.delete(id);
        return delete;
    }

    @Override
    @GetMapping("findAll")
    public List<UserDto> findAll(int limit, int page) {
//        PageHelper.startPage(page,limit);
        List<UserDto> all = userDao.findAll();
//        PageInfo<UserDto> pageInfo = new PageInfo<>(all);
        return all;
    }

    @Override
    @GetMapping("getByName")
    public UserDto getByName(String username) {
        List<UserDto> name = userDao.getByName(username);
        Assert.isTrue(name.size()==1,"用户名出错");
        return (UserDto) name.get(0);
    }

    @Override
    @PostMapping("login")
    public UserDto login(LoginParams loginParams) {
        String username = loginParams.getUsername();
        Assert.notNull(username,"账号必须不能为空");
        String password = loginParams.getPassword();
        Assert.notNull(password,"密码必须不能为空");
        // 根据用户查询用户实例
        UserDto getByName = getByName(username);
        // 验证密码
        String encode =passwordEncoder().encode(password);
        ;
        System.out.println(encode);
        boolean matches = passwordEncoder().matches(password, getByName.getPassword());
        if(matches){
            // 获取用户所有权限
            List<Permission> permissionsByUserId = getPermissionsByUserId(getByName.getId());
            // 将权限list 转化为set形式
            HashSet<Permission> Permissions = new HashSet<>(permissionsByUserId);
            // 将权限设置到用户实例中
            getByName.setAuthorities(Permissions);
            return null;
        }
        return getByName;
    }
}
